<?php

namespace App\Http\Controllers\Admin;

/**
 * 签到配置管理
 */

use App\Http\Controllers\BaseController;
use App\Services\Tools\MapService;
use Illuminate\Http\Request;                //引用http请求类
use App\Exceptions\BasicException;          //引入框架异常处理类
use App\Response\Response;

class SigninController extends BaseController
{
    /**
     * @api {get} /signin/list 签到配置列表
     * @apiVersion 0.1.0
     * @apiName 签到配置列表
     * @apiGroup 签到配置管理
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "list 签到配置列表": [{
    "id": 1,
    "days": 1,
    "type": 1,
    "prop_id": 35,
    "number": 1,
    "name": "签到7天礼包"
    }, {
    "id": 2,
    "days": 2,
    "type": 2,
    "prop_id": 6,
    "number": 30,
    "name": ""
    }],
    "reward_list 道具包列表": [{
    "id": 135,
    "name": "海洋之心"
    }, {
    "id": 134,
    "name": "帽子戏法"
    }],
    "reward_item_list 道具列表": [{
    "id": 94,
    "name": "牛转乾坤入场"
    }, {
    "id": 93,
    "name": "牛转乾坤坐骑"
    }]
    }
    }
     *
     */
    public function list()
    {
        $list = app('model_user_signin_config')
            ->select('id','days','type','prop_id','number')
            ->get()->toArray();
        if (empty($list)) {
            $config = app('model_config')->getValue('SIGN_REWARD_CONFIG');
            $data = [];
            foreach ($config as $days => $value) {
                $temp = explode('-', $value);
                $data[] = [
                    'days' => $days,
                    'type' => $temp[1],
                    'prop_id' => $temp[0],
                    'number' => $temp[2],
                    'add_time' => date('Y-m-d H:i:s')
                ];
            }
            if (!empty($data)) {
                app('model_user_signin_config')->insert($data);
            }
            $list = app('model_user_signin_config')
                ->select('id','days','type','prop_id','number')
                ->get()->toArray();
        }

        $rewardList = app('model_reward')
            ->where('active', 1)
            ->select('id', 'name')
            ->orderBy('id', 'desc')
            ->get()->toArray();

        $rewardItemList = app('model_reward_item')
            ->where('active', 1)
            ->select('id', 'name')
            ->orderBy('id', 'desc')
            ->get()->toArray();
        if (!empty($list)) {
            $rewardMap = array_column($rewardList,null,'id');
            $rewardItemMap = array_column($rewardItemList,null,'id');
            foreach ($list as $key => $value) {
                $list[$key]['name'] = $value['type']==1 ? ($rewardMap[$value['prop_id']]['name'] ?? '') : ($rewardItemMap[$value['prop_id']]['name'] ?? '');
            }
        }

        $result = [
            'list' => $list,
            'reward_list' => $rewardList,
            'reward_item_list' => $rewardItemList
        ];

        return Response::sendData($result);
    }

    /**
     * @api {post} /signin/save 签到配置保存
     * @apiVersion 0.1.0
     * @apiName 签到配置保存
     * @apiGroup 签到配置管理
     *
     * @apiParam {number} id   记录ID，必传
     * @apiParam {number} days     天数，必传
     * @apiParam {number} type     类型，必传（1：道具包，2：道具）
     * @apiParam {number} prop_id     资源ID，必传
     * @apiParam {number} number     数量，必传
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function save(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|numeric|gt:0',
                'days' => 'required|numeric|gt:0',
                'type' => 'required|numeric|in:1,2',
                'prop_id' => 'required|numeric|gt:0',
                'number' => 'required|numeric|gt:0'
            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $input = $request->input();
        $data = [
            'days' => $input['days'],
            'type' => $input['type'],
            'prop_id' => $input['prop_id'],
            'number' => $input['number']
        ];
        $id = $request->input('id', 0);
        $detail = app('model_user_signin_config')
            ->where('id', $id)->first();
        if (empty($detail)) throw new BasicException(70101);
        app('model_user_signin_config')->where('id', $id)->update($data);

        return Response::sendData([]);
    }
}
